package com.zhiyizhen.handheld.common;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.os.Bundle;

import androidx.annotation.NonNull;

import com.zhiyizhen.handheld.component.login.LoginActivity;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * description Activity管理类

 */
public class ActivityManager implements Application.ActivityLifecycleCallbacks {

    private static final ActivityManager INSTANCE = new ActivityManager();

    private final List<Activity> mActivityList = new ArrayList<>();
    private WeakReference<Activity> mCurrentActivityRef;
    private boolean isAppResumed = false;
    private boolean openThirdPartyApp = false;  // 打开三方应用

    private ActivityManager() {
    }

    public static ActivityManager getInstance() {
        return INSTANCE;
    }

    public void finishAllActivity() {
        for (Activity activity : mActivityList) {
            if (activity != null) {
                activity.finish();
            }
        }
    }

    /**
     * 关闭除登录以外的所有页面
     */
    public void finishAllWithoutLoginActivity() {
        for (Activity activity : mActivityList) {
            if (activity != null && !(activity instanceof LoginActivity)) {
                activity.finish();
            }
        }
    }

    public Activity getCurrentActivity() {
        if (mCurrentActivityRef != null && mCurrentActivityRef.get() != null) {
            return mCurrentActivityRef.get();
        }
        return null;
    }

    public Activity getBaseActivity() {
        if (!mActivityList.isEmpty()) {
            return mActivityList.get(0);
        }
        return null;
    }

    public List<Activity> getAllActivities() {
        return Collections.unmodifiableList(mActivityList);
    }

    public boolean isAppResumed() {
        return isAppResumed;
    }

    public void setOpenThirdPartyApp(boolean openThirdPartyApp) {
        this.openThirdPartyApp = openThirdPartyApp;
    }

    @Override
    public void onActivityCreated(@NonNull Activity activity, Bundle savedInstanceState) {
        synchronized (mActivityList) {
            mActivityList.add(activity);
        }
    }

    @Override
    public void onActivityStarted(@NonNull Activity activity) {
        // do nothing
    }

    @Override
    public void onActivityResumed(@NonNull Activity activity) {
        mCurrentActivityRef = new WeakReference<>(activity);
        isAppResumed = true;
    }

    @Override
    public void onActivityPaused(@NonNull Activity activity) {
        isAppResumed = false;
    }

    @Override
    public void onActivityStopped(@NonNull Activity activity) {
        final Context context = activity.getApplicationContext();
    }

    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {
        // do nothing
    }

    @Override
    public void onActivityDestroyed(@NonNull Activity activity) {
        synchronized (mActivityList) {
            mActivityList.remove(activity);
        }
    }
}
